<?php
namespace Modules\ShopApi\Models;
use Modules\ShopApi\Services\ShopUserService;
use Modules\AuthAdmin\Services\CommonService;
class ShopConsultEvaluate extends BaseApiModel
{	 
	/**
     * @name  关联用户 多对一
     */
    public function user_to()
    {
        return $this->belongsTo('Modules\ShopApi\Models\ShopUser','user_id','id');
    }
	/**
     * @name  关联下级评论 一对多
     */
    public function consult_evaluate_to()
    {
        return $this->hasMany('Modules\ShopApi\Models\ShopConsultEvaluate','pid','id');
    }
	/**
     * @name  咨询评论
     */
	 public function set_consult_evaluate($data){
		$user_info = (new ShopUserService())->my();
		if($this->insert(['user_id'=>$user_info['id'],'consult_id'=>$data['id'],'pid'=>$data['pid'],'content'=>$data['content'],'created_at'=>date('Y-m-d H:i:s')])){
			return $this->api_success();
		}else{
			return $this->api_error();
		}
	 } 
	/**
     * @name  一级评论列表
     */
	public function get_consult_evaluate_list($id){
		$list = $this->select('id','user_id','content','created_at')
				->with([
					'user_to'=>function($query){
						$query->select('id','nick_name','avatar_url');  
					},
					'consult_evaluate_to'=>function($query){
						$query->select('id','user_id','content','pid')->limit(2);  
					},
					'consult_evaluate_to.user_to'=>function($query){
						$query->select('id','nick_name','avatar_url');  
					},
				])
				->withCount([
					'consult_evaluate_to'
				])
				->where(['status'=>1,'consult_id'=>$id,'pid'=>0])
				->orderBy('id','desc')
				->paginate(10)
				->toArray();
		$CommonService = new CommonService();		
		foreach($list['data'] as $k=>$v){
			$list['data'][$k]['created_at'] = $CommonService->format_time(strtotime($v['created_at']));
		}		
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);			
	} 
	/**
     * @name  二级评论列表
     */
	public function get_consult_evaluate_one_list($data){
		$CommonService = new CommonService();
		$info = [];
		if($data['page'] == 1){
			$info = $this->select('id','user_id','content','created_at')->with([
						'user_to'=>function($query){
							$query->select('id','nick_name','avatar_url');  
						}
					])
					->find($data['pid']);	
			$info['created_at'] = $CommonService->format_time(strtotime($info['created_at']));
		}
		$list = $this->select('id','user_id','content','created_at')
				->with([
					'user_to'=>function($query){
						$query->select('id','nick_name','avatar_url');  
					}
				])
				->where(['status'=>1,'consult_id'=>$data['id'],'pid'=>$data['pid']])
				->orderBy('id','desc')
				->paginate(10)
				->toArray();
		$CommonService = new CommonService();		
		foreach($list['data'] as $k=>$v){
			$list['data'][$k]['created_at'] = $CommonService->format_time(strtotime($v['created_at']));
		}		
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total'],
			'info'          => $info
		]]);			
	} 
}
